package cn.sdormitory.common.utils;

import cn.sdormitory.basedata.dao.BClassDao;
import cn.sdormitory.basedata.entity.BClass;
import cn.sdormitory.evaluation.entity.WorkMonthInterviewEntity;
import cn.sdormitory.sys.dao.SysDeptDao;
import cn.sdormitory.sys.dao.SysUserDao;
import cn.sdormitory.sys.entity.SysDept;
import cn.sdormitory.sys.entity.SysUser;
import cn.sdormitory.sys.service.SysUserRoleService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Component
public class DataRoleUtil {
    @Resource
    SysUserRoleService sysUserRoleService;
    @Resource
    SysDeptDao sysDeptDao;
    @Resource
    SysUserDao sysUserDao;
    @Resource
    BClassDao bClassDao;

    /**
     * 根据角色来获取当前部门和部门下级的用户名
     * @return 登录用户名
     */
    public List<String> getDeptSubordinateUserList(){
        SysUser sysUser = SysUserUtils.getSysUser();
        List<Long> longs = sysUserRoleService.listRoleIdByUserId(sysUser.getId());
        List<String> collect1 = new ArrayList<>();
        if(longs.contains(5L)){
            //班主任只查自己的
            collect1.add(sysUser.getUsername());

        }
        if(longs.contains(8L)){
            //查询班级中的班主任
            LambdaQueryWrapper<BClass> wrapper = new LambdaQueryWrapper<>();
            wrapper.like(BClass::getCourseInstructorId,sysUser.getUsername());
            wrapper.eq(BClass::getStatus,"1");
            List<BClass> bClasses = bClassDao.selectList(wrapper);
            //班主任列表
            List<Long> collect = bClasses.stream().map(BClass::getClassTeacherId).collect(Collectors.toList());
            //查询班主任的用户名
            LambdaQueryWrapper<SysUser> userWrapper = new LambdaQueryWrapper<>();
            userWrapper.in(SysUser::getId,collect);
            List<SysUser> sysUsers = sysUserDao.selectList(userWrapper);
            List<String> collect2 = sysUsers.stream().map(SysUser::getUsername).collect(Collectors.toList());
            collect1.addAll(collect2);

        }
        if(longs.contains(2L)){
            //部门经理查自己的部门下全部班主任处理的任务
            Long deptId = sysUser.getDeptId();
            LambdaQueryWrapper<SysDept> deptWrapper = new LambdaQueryWrapper<>();
            deptWrapper.eq(SysDept::getPid,deptId);
            List<SysDept> sysDepts = sysDeptDao.selectList(deptWrapper);
            List<Long> collect = sysDepts.stream().map(SysDept::getId).collect(Collectors.toList());
            LambdaQueryWrapper<SysUser> userWrapper = new LambdaQueryWrapper<>();
            userWrapper.in(SysUser::getDeptId,collect);
            List<SysUser> sysUsers = sysUserDao.selectList(userWrapper);
            collect1 = sysUsers.stream().map(SysUser::getUsername).collect(Collectors.toList());
            collect1.add(sysUser.getUsername());
        }
        return collect1;
    }
}
